// subpages0/newyearGreeting/newyearGreeting.js
var app = getApp();
var bgMusicCtx = wx.createInnerAudioContext();
Page({
  data: {

  },
  onLoad: function (e) {
    var that = this;
    if (e && e.id) {
      that.setData({
        blessingId: e.id
      })
    }
    if (e && e.from) {
      that.setData({
        from: e.from
      })
    }
    if (e && e.mid) {
      that.setData({
        mid: e.mid
      })
    }
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    } else {
      that.requestBlesscfg();
      that.getSlient();
    }
  },
  onShow:function(){

  },
  onHide: function () {
    var that = this;
    bgMusicCtx.pause();
    that.setData({
      isPlaymusic: false
    })
  },
  onUnload: function () {
    var that = this;
    bgMusicCtx.stop();
    that.setData({
      isPlaymusic: false
    })
  },
  initPage: function(){
    var that = this;
    that.onLoad();
    that.onShow();
  },
  getSlient:function(){
    var that = this;
    var slient = app.globalData.slient ? app.globalData.slient:'';
    that.setData({
      slient: slient
    })
    if (slient == 1) {
      wx.hideTabBar()
    }
  },
  getBgmusic: function (e) {
    var that = this;
    var blessCfg = that.data.blessCfg;
    if (blessCfg.music) {
      bgMusicCtx.src = blessCfg.music;
      bgMusicCtx.loop = true;
      bgMusicCtx.play();
      that.setData({
        isPlaymusic: true
      })
    }
  },
  togglePlaymusic:function(){
    var that = this;
    var isPlaymusic = that.data.isPlaymusic;
    if (isPlaymusic){
      bgMusicCtx.pause();
      isPlaymusic = false;
    }else{
      bgMusicCtx.play();
      isPlaymusic = true;
    }
    that.setData({
      isPlaymusic: isPlaymusic
    })
  },
  requestBlesscfg:function(){
    var that = this;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_blessing_cfg',
        blessingId: that.data.blessingId ? that.data.blessingId:'',
        mid: that.data.mid ? that.data.mid : ''
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var blessCfg = res.data.data;
          var blessingName = '', blessingContent = '';
          var blessingId = that.data.blessingId;
          if (blessingId) {
            if (blessCfg.blessingName){
              blessingName = blessCfg.blessingName;
            }else{
              blessingName = blessCfg.blessNickname + ' ' + blessCfg.defaultName;
            }
            if (blessCfg.blessingContent) {
              blessingContent = blessCfg.blessingContent;
            } else {
              blessingContent = blessCfg.defaultContent;
            }
          }else{
            if(that.data.from == 'user'){
              blessingName = blessCfg.blessNickname + ' ' + blessCfg.defaultName;
            }else{
              blessingName = blessCfg.shopName + ' ' + blessCfg.defaultName;
            }
            blessingContent = blessCfg.defaultContent;
          }
          that.setData({
            blessCfg: blessCfg,
            blessingName: blessingName,
            blessingContent: blessingContent
          })
          if (blessCfg.title) {
            wx.setNavigationBarTitle({
              title: blessCfg.title
            });
          }
          that.getBgmusic();
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  backShouye: function () {
    app.backHome();
  },
  tomyGreet:function(){
    var that = this;
    var slient = app.globalData.slient ? app.globalData.slient : '';
    console.log('-------'+slient);
    that.setData({
      slient: slient
    })
    if (slient == 1) {
      that.setData({
        isShowgetuser: true
      })
    }else{
      wx.navigateTo({
        url: '/subpages0/yearGreeting/myGreeting/myGreeting',
      })
    }
  },
  hideGetuser: function () {
    var that = this;
    that.setData({
      isShowgetuser: false
    })
  },
  // 获取用户信息
  getuserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      var userdata = e.detail;
      console.log(userdata);
      var slient = that.data.slient;
      var again = false;
      if (slient == 0) {
        again = true;
      }
      app.getuserInfo(that, userdata, 'tomyGreet');
    }
  },
  onShareAppMessage: function () {
    var that = this;
    var blessCfg = that.data.blessCfg;
    var title = blessCfg.shopName + ' ' + blessCfg.defaultName;
    var blessingId = that.data.blessingId ? that.data.blessingId : '';
    if (blessingId){
      title = blessCfg.blessNickname + ' ' + blessCfg.defaultName;
    }
    return {
      title: title,
      path: '/subpages0/yearGreeting/shopGreeting/shopGreeting?id=' + blessingId
    }
  }
})